package com.wang.exam.controller;

import com.wang.exam.mapper.AdminMapper;
import com.wang.exam.pojo.Admin;
import com.wang.exam.pojo.Student;
import com.wang.exam.pojo.Teacher;
import com.wang.exam.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class loginController {
    @Autowired
    loginService loginService;



    @RequestMapping("/admin/main")
    public String adminLogin(@RequestParam("admin_name") String admin_name,
                             @RequestParam("admin_pwd") String admin_password,
                             Model model, HttpSession httpSession)
    {
        try{
            List<Admin> adminList = loginService.getAdminList(admin_name);
            if(adminList.get(0).getAdmin_name().equals(admin_name) && adminList.get(0).getAdmin_pwd().equals(admin_password)){
                httpSession.setAttribute("loginUser",admin_name);
                httpSession.setAttribute("admin_pwd",admin_password);
                model.addAttribute("admin_name",admin_name);
                return "admin_main";
            }else if(adminList==null){
                model.addAttribute("message","理员不存在！");
                return "index";
            }else{
                model.addAttribute("message","密码错误!");
                return "index";
            }
        }catch (Exception E){
            model.addAttribute("message","管理员不存在！");
            return "index";
        }


    }
    @RequestMapping("/teacher/main")
    public String teacherLogin(@RequestParam("teacher_id") String teacher_id,
                             @RequestParam("teacher_pwd") String teacher_pwd,
                             Model model, HttpSession httpSession)
    {
        try{
            List<Teacher> teacherList = loginService.getTeacherById(teacher_id);
            if(teacherList.get(0).getTeacher_id().equals(teacher_id) && teacherList.get(0).getTeacher_pwd().equals(teacher_pwd)){
                httpSession.setAttribute("loginTeacherId",teacherList.get(0).getTeacher_id());
                httpSession.setAttribute("teacher_pwd",teacher_pwd);
                model.addAttribute("teacher_name",teacherList.get(0).getTeacher_name());
                return "teacher_main";
            }else if(teacherList==null){
                model.addAttribute("teacher_message","教师不存在！");
                return "index";
            }else{
                model.addAttribute("teacher_message","密码错误!");
                return "index";
            }
        }catch (Exception E){
            model.addAttribute("teacher_message","教师不存在！");
            return "index";
        }
    }
    @RequestMapping("/student/main")
    public String studentLogin(@RequestParam("student_id") Integer student_id,
                               @RequestParam("student_pwd") String student_pwd,
                               Model model, HttpSession httpSession)
    {
        try{
            List<Student> studentList = loginService.getStudentById(student_id);
            if(studentList.get(0).getStudent_id().equals(student_id)  && studentList.get(0).getStudent_pwd().equals(student_pwd)){
                httpSession.setAttribute("loginStudentId",studentList.get(0).getStudent_id());
                httpSession.setAttribute("student_pwd",student_pwd);
                httpSession.setAttribute("student_id",student_id);
                httpSession.setAttribute("major_id",studentList.get(0).getMajor_id());
                model.addAttribute("student_name",studentList.get(0).getStudent_name());
                model.addAttribute("student_id",studentList.get(0).getStudent_id());
                return "student_main";
            }else if(studentList==null){
                model.addAttribute("student_message","学生不存在！");
                return "index";
            }else{
                model.addAttribute("student_message","密码错误!");
                return "index";
            }
        }catch (Exception E){
            model.addAttribute("student_message","学生不存在！");
            return "index";
        }


    }
    @RequestMapping("/")
    public String index() {
        return "index";
    }

}
